package com.weiiew.backend.operationUtils;

import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.jnativehook.keyboard.NativeKeyEvent;

import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 键盘按键映射枚举（包含功能说明）
 * @Author 魏锴
 * @Date 2025/4/26
 */
@Slf4j
@ToString
public enum KeyCode {
    // ================= 字母键 =================
    A(NativeKeyEvent.VC_A, KeyEvent.VK_A),
    B(NativeKeyEvent.VC_B, KeyEvent.VK_B),
    C(NativeKeyEvent.VC_C, KeyEvent.VK_C),
    D(NativeKeyEvent.VC_D, KeyEvent.VK_D),
    E(NativeKeyEvent.VC_E, KeyEvent.VK_E),
    F(NativeKeyEvent.VC_F, KeyEvent.VK_F),
    G(NativeKeyEvent.VC_G, KeyEvent.VK_G),
    H(NativeKeyEvent.VC_H, KeyEvent.VK_H),
    I(NativeKeyEvent.VC_I, KeyEvent.VK_I),
    J(NativeKeyEvent.VC_J, KeyEvent.VK_J),
    K(NativeKeyEvent.VC_K, KeyEvent.VK_K),
    L(NativeKeyEvent.VC_L, KeyEvent.VK_L),
    M(NativeKeyEvent.VC_M, KeyEvent.VK_M),
    N(NativeKeyEvent.VC_N, KeyEvent.VK_N),
    O(NativeKeyEvent.VC_O, KeyEvent.VK_O),
    P(NativeKeyEvent.VC_P, KeyEvent.VK_P),
    Q(NativeKeyEvent.VC_Q, KeyEvent.VK_Q),
    R(NativeKeyEvent.VC_R, KeyEvent.VK_R),
    S(NativeKeyEvent.VC_S, KeyEvent.VK_S),
    T(NativeKeyEvent.VC_T, KeyEvent.VK_T),
    U(NativeKeyEvent.VC_U, KeyEvent.VK_U),
    V(NativeKeyEvent.VC_V, KeyEvent.VK_V),
    W(NativeKeyEvent.VC_W, KeyEvent.VK_W),
    X(NativeKeyEvent.VC_X, KeyEvent.VK_X),
    Y(NativeKeyEvent.VC_Y, KeyEvent.VK_Y),
    Z(NativeKeyEvent.VC_Z, KeyEvent.VK_Z),

    // ================= 主键盘数字键 =================
    NUM0(NativeKeyEvent.VC_0, KeyEvent.VK_0),
    NUM1(NativeKeyEvent.VC_1, KeyEvent.VK_1),
    NUM2(NativeKeyEvent.VC_2, KeyEvent.VK_2),
    NUM3(NativeKeyEvent.VC_3, KeyEvent.VK_3),
    NUM4(NativeKeyEvent.VC_4, KeyEvent.VK_4),
    NUM5(NativeKeyEvent.VC_5, KeyEvent.VK_5),
    NUM6(NativeKeyEvent.VC_6, KeyEvent.VK_6),
    NUM7(NativeKeyEvent.VC_7, KeyEvent.VK_7),
    NUM8(NativeKeyEvent.VC_8, KeyEvent.VK_8),
    NUM9(NativeKeyEvent.VC_9, KeyEvent.VK_9),

    // ================= 功能键 =================
    /** F1 通用帮助键/IDE调试 */
    F1(NativeKeyEvent.VC_F1, KeyEvent.VK_F1),
    /** F2 重命名/Excel编辑 */
    F2(NativeKeyEvent.VC_F2, KeyEvent.VK_F2),
    /** F3 查找下一个 */
    F3(NativeKeyEvent.VC_F3, KeyEvent.VK_F3),
    /** F4 地址栏/关闭窗口 */
    F4(NativeKeyEvent.VC_F4, KeyEvent.VK_F4),
    /** F5 刷新/幻灯片播放 */
    F5(NativeKeyEvent.VC_F5, KeyEvent.VK_F5),
    /** F6 地址栏切换 */
    F6(NativeKeyEvent.VC_F6, KeyEvent.VK_F6),
    /** F7 拼写检查 */
    F7(NativeKeyEvent.VC_F7, KeyEvent.VK_F7),
    /** F8 启动菜单/调试 */
    F8(NativeKeyEvent.VC_F8, KeyEvent.VK_F8),
    /** F9 编译/邮件发送 */
    F9(NativeKeyEvent.VC_F9, KeyEvent.VK_F9),
    /** F10 菜单激活 */
    F10(NativeKeyEvent.VC_F10, KeyEvent.VK_F10),
    /** F11 全屏切换 */
    F11(NativeKeyEvent.VC_F11, KeyEvent.VK_F11),
    /** F12 开发者工具 */
    F12(NativeKeyEvent.VC_F12, KeyEvent.VK_F12),

    // ================= 控制键 =================
    /** Enter 确认输入/执行命令 */
    ENTER(NativeKeyEvent.VC_ENTER, KeyEvent.VK_ENTER),
    /** Esc 取消/退出菜单 */
    ESCAPE(NativeKeyEvent.VC_ESCAPE, KeyEvent.VK_ESCAPE),
    /** 空格键 选择/暂停 */
    SPACE(NativeKeyEvent.VC_SPACE, KeyEvent.VK_SPACE),
    /** Tab 焦点切换/缩进 */
    TAB(NativeKeyEvent.VC_TAB, KeyEvent.VK_TAB),
    /** Backspace 删除前字符 */
    BACKSPACE(NativeKeyEvent.VC_BACKSPACE, KeyEvent.VK_BACK_SPACE),
    /** Delete 删除后字符 */
    DELETE(NativeKeyEvent.VC_DELETE, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_DELETE),
    /** Insert 插入模式切换 */
    INSERT(NativeKeyEvent.VC_INSERT, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_INSERT),
    /** Home 跳转至开头 */
    HOME(NativeKeyEvent.VC_HOME, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_HOME),
    /** End 跳转至结尾 */
    END(NativeKeyEvent.VC_END, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_END),
    /** PageUp 向上翻页 */
    PAGE_UP(NativeKeyEvent.VC_PAGE_UP, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_PAGE_UP),
    /** PageDown 向下翻页 */
    PAGE_DOWN(NativeKeyEvent.VC_PAGE_DOWN, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_PAGE_DOWN),

    // ================= 修饰键 =================
    /** 左Shift 大小写切换 */
    LEFT_SHIFT(NativeKeyEvent.VC_SHIFT, NativeKeyEvent.KEY_LOCATION_LEFT, KeyEvent.VK_SHIFT),
    /** 右Shift 专用于组合键 */
    RIGHT_SHIFT(3638, NativeKeyEvent.KEY_LOCATION_RIGHT, KeyEvent.VK_SHIFT),
    /** 左Ctrl 通用快捷键 */
    LEFT_CONTROL(NativeKeyEvent.VC_CONTROL, NativeKeyEvent.KEY_LOCATION_LEFT, KeyEvent.VK_CONTROL),
    /** 右Ctrl 应用特定功能 */
    RIGHT_CONTROL(NativeKeyEvent.VC_CONTROL, NativeKeyEvent.KEY_LOCATION_RIGHT, KeyEvent.VK_CONTROL),
    /** 左Alt 菜单激活 */
    LEFT_ALT(NativeKeyEvent.VC_ALT, NativeKeyEvent.KEY_LOCATION_LEFT, KeyEvent.VK_ALT),
    /** 右Alt Gr键（国际键盘） */
    RIGHT_ALT(NativeKeyEvent.VC_ALT, NativeKeyEvent.KEY_LOCATION_RIGHT, KeyEvent.VK_ALT_GRAPH),
    /** Meta键（Mac Command） */
    META(NativeKeyEvent.VC_META, NativeKeyEvent.KEY_LOCATION_LEFT, KeyEvent.VK_META),

    // ================= 方向键 =================
    /** 上方向键 向上导航 */
    UP(NativeKeyEvent.VC_UP, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_UP),
    /** 下方向键 向下导航 */
    DOWN(NativeKeyEvent.VC_DOWN, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_DOWN),
    /** 左方向键 向后导航 */
    LEFT(NativeKeyEvent.VC_LEFT, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_LEFT),
    /** 右方向键 向前导航 */
    RIGHT(NativeKeyEvent.VC_RIGHT, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_RIGHT),

    // ================= 小键盘关闭NUM_LOCK后的方向键 =================
    /** 上方向键 向上导航 */
    NUM_UP(NativeKeyEvent.VC_UP, KeyEvent.VK_UP),
    /** 下方向键 向下导航 */
    NUM_DOWN(NativeKeyEvent.VC_DOWN, KeyEvent.VK_DOWN),
    /** 左方向键 向后导航 */
    NUM_LEFT(NativeKeyEvent.VC_LEFT, KeyEvent.VK_LEFT),
    /** 右方向键 向前导航 */
    NUM_RIGHT(NativeKeyEvent.VC_RIGHT, KeyEvent.VK_RIGHT),

    /** Delete 删除后字符 */
    NUM_DELETE(NativeKeyEvent.VC_DELETE, KeyEvent.VK_DELETE),
    /** Insert 插入模式切换 */
    NUM_INSERT(NativeKeyEvent.VC_INSERT, KeyEvent.VK_INSERT),

    /** Home 跳转至开头 */
    NUM_HOME(NativeKeyEvent.VC_HOME, KeyEvent.VK_HOME),
    /** End 跳转至结尾 */
    NUM_END(NativeKeyEvent.VC_END, KeyEvent.VK_END),
    /** PageUp 向上翻页 */
    NUM_PAGE_UP(NativeKeyEvent.VC_PAGE_UP, KeyEvent.VK_PAGE_UP),
    /** PageDown 向下翻页 */
    NUM_PAGE_DOWN(NativeKeyEvent.VC_PAGE_DOWN, KeyEvent.VK_PAGE_DOWN),

    // ================= 符号键 =================
    /** `~键 控制台/代码块 */
    GRAVE(NativeKeyEvent.VC_BACKQUOTE, KeyEvent.VK_BACK_QUOTE),
    /** -_键 减号/下划线 */
    MINUS(NativeKeyEvent.VC_MINUS, KeyEvent.VK_MINUS),
    /** =+键 等于/加号 */
    EQUALS(NativeKeyEvent.VC_EQUALS, KeyEvent.VK_EQUALS),
    /** [{键 代码块开始 */
    OPEN_BRACKET(NativeKeyEvent.VC_OPEN_BRACKET, KeyEvent.VK_OPEN_BRACKET),
    /** ]}键 代码块结束 */
    CLOSE_BRACKET(NativeKeyEvent.VC_CLOSE_BRACKET, KeyEvent.VK_CLOSE_BRACKET),
    /** \|键 路径/转义符 */
    BACKSLASH(NativeKeyEvent.VC_BACK_SLASH, KeyEvent.VK_BACK_SLASH),
    /** ;:键 语句结束 */
    SEMICOLON(NativeKeyEvent.VC_SEMICOLON, KeyEvent.VK_SEMICOLON),
    /** '"键 字符串定义 */
    QUOTE(NativeKeyEvent.VC_QUOTE, KeyEvent.VK_QUOTE),
    /** ,<键 分隔符 */
    COMMA(NativeKeyEvent.VC_COMMA, KeyEvent.VK_COMMA),
    /** .>键 对象访问 */
    PERIOD(NativeKeyEvent.VC_PERIOD, KeyEvent.VK_PERIOD),
    /** /?键 路径/帮助 */
    SLASH(NativeKeyEvent.VC_SLASH, KeyEvent.VK_SLASH),

    // ================= 小键盘 =================
    /** 小键盘0 数字输入 */
    NUMPAD0(NativeKeyEvent.VC_0, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD0),
    /** 小键盘1 数字输入/结束 */
    NUMPAD1(NativeKeyEvent.VC_1, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD1),
    /** 小键盘2 数字输入/下方向 */
    NUMPAD2(NativeKeyEvent.VC_2, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD2),
    /** 小键盘3 数字输入/下一页 */
    NUMPAD3(NativeKeyEvent.VC_3, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD3),
    /** 小键盘4 数字输入/左方向 */
    NUMPAD4(NativeKeyEvent.VC_4, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD4),
    /** 小键盘5 数字输入/居中 */
    NUMPAD5(NativeKeyEvent.VC_5, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD5),
    /** 小键盘6 数字输入/右方向 */
    NUMPAD6(NativeKeyEvent.VC_6, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD6),
    /** 小键盘7 数字输入/Home */
    NUMPAD7(NativeKeyEvent.VC_7, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD7),
    /** 小键盘8 数字输入/上方向 */
    NUMPAD8(NativeKeyEvent.VC_8, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD8),
    /** 小键盘9 数字输入/PgUp */
    NUMPAD9(NativeKeyEvent.VC_9, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUMPAD9),
    /** 小键盘+ 数学运算 */
    NUMPAD_ADD(3662, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_ADD),
    /** 小键盘- 数学运算 */
    NUMPAD_SUBTRACT(3658, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_SUBTRACT),
    /** 小键盘* 数学运算 */
    NUMPAD_MULTIPLY(3639, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_MULTIPLY),
    /** 小键盘/ 数学运算 */
    NUMPAD_DIVIDE(53, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_DIVIDE),
    /** 小键盘回车 数字确认 */
    NUMPAD_ENTER(NativeKeyEvent.VC_ENTER, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_ENTER),
    /** 小键盘分隔符 千位分隔 */
    NUMPAD_SEPARATOR(NativeKeyEvent.VC_SEPARATOR, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_SEPARATOR),

    // ================= 功能锁定键 =================
    /** 大写锁定 切换大小写 */
    CAPS_LOCK(NativeKeyEvent.VC_CAPS_LOCK, KeyEvent.VK_CAPS_LOCK),
    /** 数字锁定 切换小键盘 */
    NUM_LOCK(NativeKeyEvent.VC_NUM_LOCK, NativeKeyEvent.KEY_LOCATION_NUMPAD, KeyEvent.VK_NUM_LOCK),
    /** 滚动锁定 表格导航 */
    SCROLL_LOCK(NativeKeyEvent.VC_SCROLL_LOCK, KeyEvent.VK_SCROLL_LOCK),
    /** 截屏键 屏幕捕获 */
    PRINT_SCREEN(NativeKeyEvent.VC_PRINTSCREEN, KeyEvent.VK_PRINTSCREEN),
    /** 暂停键 暂停进程 */
    PAUSE(NativeKeyEvent.VC_PAUSE, KeyEvent.VK_PAUSE);
    // ================= 媒体键,robot类并不支持该操作 =================
//    VOLUME_UP(NativeKeyEvent.VC_VOLUME_UP, KeyEvent.VK_VOLUME_UP),
//    VOLUME_DOWN(NativeKeyEvent.VC_VOLUME_DOWN, KeyEvent.VK_VOLUME_DOWN),
//    VOLUME_MUTE(NativeKeyEvent.VC_MUTE, KeyEvent.VK_MUTE),
//    MEDIA_PLAY(NativeKeyEvent.VC_MEDIA_PLAY,NativeKeyEvent.KEY_LOCATION_STANDARD),
//    MEDIA_STOP(NativeKeyEvent.VC_MEDIA_STOP, KeyEvent.VK_STOP),
//    MEDIA_NEXT(NativeKeyEvent.VC_MEDIA_NEXT, KeyEvent.VK_NEXT),
//    MEDIA_PREVIOUS(NativeKeyEvent.VC_MEDIA_PREVIOUS, KeyEvent.VK_PREVIOUS);

    // ================= 字段和方法 =================
    private final int nativeCode;
    private final int location;
    private final int robotCode;

    private static final Map<String, KeyCode> keyMapping = new HashMap<>();

    static {
        for (KeyCode key : values()) {
            String compositeKey = createCompositeKey(key.nativeCode, key.location);
            keyMapping.put(compositeKey, key);
        }
    }

    private static String createCompositeKey(int nativeCode, int location) {
        return nativeCode + "|" + location;
    }

    KeyCode(int nativeCode, int location, int robotCode) {
        this.nativeCode = nativeCode;
        this.location = location;
        this.robotCode = robotCode;
    }

    KeyCode(int nativeCode, int robotCode) {
        this(nativeCode, NativeKeyEvent.KEY_LOCATION_STANDARD, robotCode);
    }

    public int getNativeCode() {
        return nativeCode;
    }

    public int getRobotCode() {
        return robotCode;
    }

    public static KeyCode fromNativeEvent(NativeKeyEvent event) {
        int nativeCode = event.getKeyCode();
        int location = event.getKeyLocation();
//        if (!keyMapping.containsKey(createCompositeKey(nativeCode, location))) {
//            for (int i = 1; i <= 4; i++){
//                log.info("location遍历尝试: {}->{}", i, keyMapping.get(createCompositeKey(nativeCode, i)));
//            }
//        }
//        log.info("转化参数: code={}, location={}", event.getKeyCode(), event.getKeyLocation());
        return keyMapping.getOrDefault(createCompositeKey(nativeCode, location), null);
    }
}